package cn.com.template.controller.budgetcalc;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.commons.Paginator;
import cn.com.commons.utils.StringUtil;
import cn.com.dal.dataobject.cs.OutsidecostDO;
import cn.com.dal.dataobject.hr.DeptDO;
import cn.com.dal.dataobject.sys.DictDO;
import cn.com.service.cs.OutsidecostService;
import cn.com.service.hr.DeptService;
import cn.com.service.sys.DictService;
import cn.com.support.context.UserContext;
import cn.com.support.message.MessageResult;
import cn.com.support.utils.JacksonUtil;
import cn.com.support.utils.TemplateUtils;
import cn.com.template.ui.Flexigrid;
import cn.com.template.utils.FlexigridUtil;

/**
 * 经营外委费用控制器
 * 
 * @since 2012-4-2 下午8:13:44
 * @version 1.0
 * @author Microbun
 * 
 */
@Controller
@RequestMapping("/budgetcalc/outsidecost")
public class OutsidecostController {

	@Autowired
	private OutsidecostService outsidecostService;
	
	@Autowired
	private DictService dictService;
	
	@Autowired
	private DeptService deptService;
	
	/**
	 * 跳转页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping
	public ModelAndView forward(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/budgetcalc/outsidecost");
		DeptDO deptDO = deptService.selectById(UserContext.getUser().getDeptId());
		DictDO dictDO = new DictDO();
		if(null != deptDO){
			dictDO.setModule(deptDO.getName());
		}
		List<DictDO> dictLst = dictService.selectList(dictDO);
		request.setAttribute("dictLst", dictLst);
		return mav;
	}

	/**
	 * 删除数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delOutsideCost")
	@ResponseBody
	public MessageResult<Long> delete(@RequestParam Long[] ids) {
		MessageResult<Long> result = null;
		try {
			result = outsidecostService.deleteById(ids);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 查询所有
	 * 
	 * @param condition
	 *            查询条件
	 * @return
	 */
	@RequestMapping("/selectList")
	@ResponseBody
	public List<?> selectList(HttpServletRequest request,
			HttpServletResponse response) {
		return outsidecostService.selectList(TemplateUtils.bulidBeanMap(
				request, OutsidecostDO.class));
	}

	/**
	 * 根据ID查询
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/selectById")
	@ResponseBody
	public MessageResult<OutsidecostDO> selectById(@RequestParam("id") Long id) {
		return outsidecostService.selectById(id);
	}

	/**
	 * 分页查询
	 * 
	 * @param paginator
	 * @return
	 */
	@RequestMapping("/selectForPage.json")
	@ResponseBody
	public Flexigrid selectForPage(HttpServletRequest request) {
		Flexigrid flexigrid = null;
		try {
			Paginator<OutsidecostDO, List<OutsidecostDO>> paginator = new Paginator<OutsidecostDO, List<OutsidecostDO>>();
			String projectName = request.getParameter("projectName");
			OutsidecostDO outsidecostDO = new OutsidecostDO();
			if(StringUtil.isNotBlank(projectName)){
				outsidecostDO.setProjectName(projectName);
			}
			paginator.setCondition(outsidecostDO);
			// 设置分页器
			paginator.setPageSize(Integer.parseInt(request.getParameter("rp")));
			paginator.setCurrentPage(Integer.parseInt(request.getParameter("page")));
			outsidecostService.selectByPage(paginator);
			//PACKAGE FLEXIGRID DATA
			flexigrid = FlexigridUtil.formatFlexigridData(paginator);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flexigrid;
	}

	/**
	 * 更新费用
	 * 
	 * @param projectDO
	 * @return
	 */
	@RequestMapping("/updateOutsideCost")
	@ResponseBody
	MessageResult<Integer> updateById(HttpServletRequest request) {
		MessageResult<Integer> result = null;
		OutsidecostDO outsidecostDO = null;
		try {
			String jsonData = request.getParameter("data");
			outsidecostDO = (OutsidecostDO) JacksonUtil.json2Bean(jsonData, OutsidecostDO.class);
			outsidecostDO.setProjectId(null);
			outsidecostDO.setModifier(UserContext.getUsername());
			result = outsidecostService.updateById(outsidecostDO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 新增费用
	 * 
	 * @param projectDO
	 * @return
	 */
	@RequestMapping("/addOutsideCost")
	@ResponseBody
	MessageResult<Long> insert(HttpServletRequest request) {
		MessageResult<Long> result = null;
		OutsidecostDO outsidecostDO = null;
		try {
			String jsonData = request.getParameter("data");
			String username = UserContext.getUsername();
			//防止js harmful数据入库
			jsonData = jsonData.replaceAll("\"", "\\\"");
			outsidecostDO = (OutsidecostDO) JacksonUtil.json2Bean(jsonData, OutsidecostDO.class);
			outsidecostDO.setCreator(username);
			result = outsidecostService.insert(outsidecostDO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
